//
//  WGBAlertManager.m
//  WebClipBoardDemo
//
//  Created by 王贵彬  on 2023/5/12.
//

#import "WGBAlertManager.h"

@interface WGBAlertManager ()

@property (nonatomic, strong) WGBAlertView *alertView;
@property (nonatomic,strong) NSMutableArray *dataSource;

@end

@implementation WGBAlertManager

+ (WGBAlertManager *)manager{
    static WGBAlertManager *_manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[self alloc] init];
    });
    return _manager;
}

- (WGBAlertView *)alertView{
    if(!_alertView){
        _alertView = [WGBAlertView alertView];
        _alertView.isUsingSpring = NO;
        _alertView.touchDismiss = YES;
    }
    return _alertView;
}

- (void)setAlertViewsArray:(NSArray<UIView *> *)alertViewsArray{
    _alertViewsArray = alertViewsArray;
    self.dataSource = alertViewsArray.mutableCopy;
}

- (void)setContentView:(UIView *)contentView{
    _contentView = contentView;
    self.dataSource = @[contentView].mutableCopy;
}

- (void)show{
    [self showAtHostView:nil];
}

/// 将弹窗挂载到指定父视图上 hostView为空会默认添加到keyWindow
- (void)showAtHostView:(UIView *_Nullable)hostView{
    if(!self.dataSource.count){
        return;
    }
    __block UIView *view = self.dataSource.firstObject;
    self.alertView.maskColor = self.maskColor;
    self.alertView.contentView = view;
    self.alertView.animationType = self.animationType;
    [self.alertView showAtHostView:hostView];
    __weak typeof(self) weak_self = self;
    [self.alertView setDismissDidEndBlock:^{
        [weak_self.dataSource removeObject:view];
        [weak_self showAtHostView:hostView];
    }];
}

- (void)dismiss{
    [self.alertView dismiss];
}

@end
